package com.module.product.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;

import com.module.product.vo.AttrItem;
import com.module.product.vo.AttrValueTemporary;

public class AttrValueTemporaryDao extends GenericHibernateDao<AttrValueTemporary, String> {
    private static final AttrValueTemporaryDao instance = new AttrValueTemporaryDao();

    private AttrValueTemporaryDao() {
    }

    public synchronized static AttrValueTemporaryDao getInstance() {
        return instance;
    }
    
    public List<AttrItem> listByCategoryId(Connection conn,String categoryId,String productId) throws SQLException{
        List<AttrItem> list  = new ArrayList<AttrItem> ();
        AttrItem attr = null;
        PreparedStatement stmt = null ;
        try
        {
        String sql = "select a.c_id,a.c_name,b.c_attr_value ,a.c_groupId,a.c_desc ,b.c_product_id,a.c_type,b.i_attr_value_size " +
        		"from p_attr_item a left join p_attr_value_temporary b on a.c_id = b.c_attritem_id  and b.c_product_id =?" +
        		" where a.c_status !=? and a.c_groupId in (select c_id from p_attr_group where c_category_id = ? ) ";
        stmt = conn.prepareStatement(sql);
                          stmt.setString(1,productId);
                          stmt.setString(2,CUDEnum.D.toString());
                          stmt.setString(3,categoryId);
        ResultSet rs  = stmt.executeQuery();  
        
        while(rs.next()){ 
            attr = new AttrItem();
            attr.setId(rs.getString(1));
            attr.setName(rs.getString(2));
            attr.setAttrValue(rs.getString(3));
            attr.setAttrGroup(rs.getString(4));
            attr.setDesc(rs.getString(5));
            attr.setType(rs.getString(7));
            attr.setAttrValueSize(rs.getInt(8));
            list.add(attr);
           }                  
        }finally{
        	this.close(stmt);
        }
        return list;
    }
    public List<AttrValueTemporary> findAttrByProductId(Session session,String productId){
    	try {
			Query query=session.createQuery(" from AttrValueTemporary where productId='"+productId+"'");
			return query.list();
    	} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
}
